package org.shangrila.demo.pf.mb;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.PrimeFaces;

@Named
@SessionScoped
public class RequestContextView {

	private DemoOperator operator;

	@PostConstruct
	public void init() {
		operator = new DemoOperator();
		if (!FacesContext.getCurrentInstance().isPostback()) {
			PrimeFaces.current().executeScript("alert('This onload script is added from backing bean.')");
		}
	}

	public void save() {
		PrimeFaces.current().ajax().addCallbackParam("saved", true); // basic parameter
		PrimeFaces.current().ajax().addCallbackParam("operator", operator); // pojo as json

		// execute javascript oncomplete
		PrimeFaces.current().executeScript("PrimeFaces.info('Hello from the Backing Bean');");

		// update panel
		PrimeFaces.current().ajax().update(":form:panel");

		// scroll to panel
		PrimeFaces.current().scrollTo(":form:panel");

		// add facesmessage
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Success", "Success"));
	}


	public DemoOperator getOperator() {
		return operator;
	}

	public void setOperator(DemoOperator operator) {
		this.operator = operator;
	}
}
